#import "layer_menu_main.h"

#define MENU_MAIN_Z_BACKGROUND 0
#define MENU_MAIN_Z_CLOUD 1
#define MENU_MAIN_Z_LIGHT 2
#define MENU_MAIN_Z_TITLE 3
#define MENU_MAIN_Z_CUPID 4
#define MENU_MAIN_Z_MENU 5

#define CLOUD_BASE_Y 214
#define CLOUD_BASE_Y_OFFSET 25

@implementation LayerMenuMain

@synthesize flagLockFunction = flagLockMenuFunction_;

- (id)initLayerMenuMain
{
	id _ActionHeartAni;

	if ((self = [super init]))
	{
		/*load resource
		*/
		reLoadResource(&g_CfgMenuMainResourceTable);

		/*image
		*/
		imageBackground_ = uiCreateImageObject(&g_CfgMenuMainImageBackground, self);
		imageTitle_ = uiCreateImageObject(&g_CfgMenuMainImageTitle, self);

		/*cloud
		*/
		cloud1_ = uiCreateImageObject(&g_CfgMenuMainImageCloud1, self);
		cloud2_ = uiCreateImageObject(&g_CfgMenuMainImageCloud2, self);
		cloud3_ = uiCreateImageObject(&g_CfgMenuMainImageCloud3, self);

		[self CreateCloud];

		/*heart
		*/
		heart1_ = [[CCSprite alloc] init];
		heart2_ = [[CCSprite alloc] init];
		heart3_ = [[CCSprite alloc] init];
		[self addChild:heart1_];
		[self addChild:heart2_];
		[self addChild:heart3_];
		[heart1_ release];
		[heart2_ release];
		[heart3_ release];
		heart1_.scale = 0.8f;
		heart2_.scale = 0.7f;
		heart3_.scale = 0.5f;
		_ActionHeartAni = [CCAnimate actionWithAnimation:uiCreateAnimationObject(&g_CfgMenuMainAniHeart)];
		[heart1_ runAction:[CCRepeatForever actionWithAction:_ActionHeartAni]];
		_ActionHeartAni = [CCAnimate actionWithAnimation:uiCreateAnimationObject(&g_CfgMenuMainAniHeart)];
		[heart2_ runAction:[CCRepeatForever actionWithAction:_ActionHeartAni]];
		_ActionHeartAni = [CCAnimate actionWithAnimation:uiCreateAnimationObject(&g_CfgMenuMainAniHeart)];
		[heart3_ runAction:[CCRepeatForever actionWithAction:_ActionHeartAni]];
		heart1_.positionInPixels = g_CfgMenuMainHeart1Position;
		heart2_.positionInPixels = g_CfgMenuMainHeart2Position;
		heart3_.positionInPixels = g_CfgMenuMainHeart3Position;

		/*menu
		*/
		itemPlay_ = uiCreateMenuItemImageObject(&g_CfgMenuMainItemPlay, self, @selector(CallBackMenuItemPlay:));
		itemLoveLibrary_ = uiCreateMenuItemImageObject(&g_CfgMenuMainItemLoveLibrary, self, @selector(CallBackMenuItemLoveLibrary:));
		itemSurvival_ = uiCreateMenuItemImageObject(&g_CfgMenuMainItemSurvival, self, @selector(CallBackMenuItemSurvival:));
		itemOption_ = uiCreateMenuItemImageObject(&g_CfgMenuMainItemOption, self, @selector(CallBackMenuItemOption:));
		itemGameCenter_ = uiCreateMenuItemImageObject(&g_CfgMenuMainItemGameCenter, self, @selector(CallBackMenuItemGameCenter:));
		itemMoreGames_ = uiCreateMenuItemImageObject(&g_CfgMenuMainItemMoreGames, self, @selector(CallBackMenuItemMoreGames:));

		ccColor3B _Color = {50, 50, 50};
		itemSurvival_.color = _Color;
		itemSurvival_.opacity = 80;
		itemGameCenter_.color = _Color;
		itemSurvival_.opacity = 80;

		itemFunctionSprite_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgMenuMainItemFunction.path_normal]];
		itemFunction_ = [CCMenuItemImage itemFromNormalSprite:itemFunctionSprite_ selectedSprite:nil disabledSprite:nil target:self selector:@selector(CallBackMenuItemFunction:)];
		itemFunction_.positionInPixels = g_CfgMenuMainItemFunction.position;

		menu_ = uiNodeAddMenu(self, CGPointZero, itemPlay_, itemLoveLibrary_, itemSurvival_, itemFunction_, itemOption_, itemGameCenter_, itemMoreGames_, nil);

		[itemOption_ runAction:[CCHide action]];
		[itemGameCenter_ runAction:[CCHide action]];
		[itemMoreGames_ runAction:[CCHide action]];

		/*flag
		*/
		flagLockMenuFunction_ = false;
		flagLockItemFunction_ = false;
	}
	return self;
}

- (void)dealloc
{
	/*release resource
	*/
	reFreeResource(&g_CfgMenuMainResourceTable);

	[super dealloc];
}

- (void)tickFunctionBack:(ccTime)Time
{
	CCSequence *_Seq;
	id _Ani;
	CCCallFuncN *_CallBack;

	[self unschedule:@selector(tickFunctionBack:)];

	[itemFunction_ runAction:[CCShow action]];
	[itemOption_ runAction:[CCHide action]];
	[itemGameCenter_ runAction:[CCHide action]];
	[itemMoreGames_ runAction:[CCHide action]];

	flagLockItemFunction_ = true;

	_Ani = [CCAnimate actionWithAnimation:uiCreateAnimationReverseObject(&g_CfgMenuMainAniFunction)];
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackItemFunctionBackAniOver:)];
	_Seq = [CCSequence actions:_Ani, _CallBack, nil];
	[itemFunctionSprite_ runAction:_Seq];
}

- (void)CreateCloud
{
	int i;
	float _Y;
	float _Time;
	float _Offset;
	CCSprite *_Image, *_ImageDummy;
	action_move_in_pixel *_ActionMove1, *_ActionMove2;
	CCSequence *_Seq;
	CCRepeatForever *_Rep;

	_Y = CLOUD_BASE_Y;
	_Time = 600;
	for (i = 0; i < 8; ++i)
	{
		if (i % 2 == 0)
		{
			_Image = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgMenuMainImageCloudWhitePath]];
			_ImageDummy = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgMenuMainImageCloudWhitePath]];

			_Offset = rand() % (int)(g_ScrrenSize.width - 10.0f);
			_Image.positionInPixels = ccp(g_ScrrenSize.width * 0.5 + _Offset, _Y);
			_ImageDummy.positionInPixels = ccp(g_ScrrenSize.width * -0.5 + _Offset, _Y);

			_ActionMove1 = [action_move_in_pixel actionWithDuration:_Time position:ccp(g_ScrrenSize.width * 1.5, _Y)];
			_ActionMove2 = [action_move_in_pixel actionWithDuration:0 position:ccp(g_ScrrenSize.width * 0.5, _Y)];
			_Seq = [CCSequence actions:_ActionMove1, _ActionMove2, nil];
			_Rep = [CCRepeatForever actionWithAction:_Seq];
			[_Image runAction:_Rep];

			_ActionMove1 = [action_move_in_pixel actionWithDuration:_Time position:ccp(g_ScrrenSize.width * 0.5, _Y)];
			_ActionMove2 = [action_move_in_pixel actionWithDuration:0 position:ccp(g_ScrrenSize.width * -0.5, _Y)];
			_Seq = [CCSequence actions:_ActionMove1, _ActionMove2, nil];
			_Rep = [CCRepeatForever actionWithAction:_Seq];
			[_ImageDummy runAction:_Rep];
		}
		else
		{
			_Image = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgMenuMainImageCloudBluePath]];
			_ImageDummy = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgMenuMainImageCloudBluePath]];

			_Offset = rand() % (int)(g_ScrrenSize.width - 10.0f);
			_Image.positionInPixels = ccp(g_ScrrenSize.width * 0.5 - _Offset, _Y);
			_ImageDummy.positionInPixels = ccp(g_ScrrenSize.width * 1.5 - _Offset, _Y);

			_ActionMove1 = [action_move_in_pixel actionWithDuration:_Time position:ccp(g_ScrrenSize.width * -0.5, _Y)];
			_ActionMove2 = [action_move_in_pixel actionWithDuration:0 position:ccp(g_ScrrenSize.width * 0.5, _Y)];
			_Seq = [CCSequence actions:_ActionMove1, _ActionMove2, nil];
			_Rep = [CCRepeatForever actionWithAction:_Seq];
			[_Image runAction:_Rep];

			_ActionMove1 = [action_move_in_pixel actionWithDuration:_Time position:ccp(g_ScrrenSize.width * 0.5, _Y)];
			_ActionMove2 = [action_move_in_pixel actionWithDuration:0 position:ccp(g_ScrrenSize.width * 1.5, _Y)];
			_Seq = [CCSequence actions:_ActionMove1, _ActionMove2, nil];
			_Rep = [CCRepeatForever actionWithAction:_Seq];
			[_ImageDummy runAction:_Rep];
		}

		[self addChild:_Image];
		[self addChild:_ImageDummy];
		_Y -= CLOUD_BASE_Y_OFFSET;
		_Time -= 70;
	}
}

- (void)CallBackMenuItemPlay:(id)Sender
{
	if (flagLockMenuFunction_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	[g_SceneMenu SetState:SCENE_MENU_STATE_CHAPTER];
}

- (void)CallBackMenuItemLoveLibrary:(id)Sender
{
	if (flagLockMenuFunction_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	[g_SceneMenu SceneChangeToLibrary];
}

- (void)CallBackMenuItemSurvival:(id)Sender
{
	return;

	if (flagLockMenuFunction_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();
}

- (void)CallBackMenuItemFunction:(id)Sender
{
	CCSequence *_Seq;
	id _Ani;
	CCCallFuncN *_CallBack;

	if (flagLockMenuFunction_)
	{
		return;
	}
	if (flagLockItemFunction_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	flagLockItemFunction_ = true;
	_Ani = [CCAnimate actionWithAnimation:uiCreateAnimationObject(&g_CfgMenuMainAniFunction)];
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackItemFunctionAniOver:)];
	_Seq = [CCSequence actions:_Ani, _CallBack, nil];
	[itemFunctionSprite_ runAction:_Seq];
}

- (void)CallBackMenuItemOption:(id)Sender
{
	if (flagLockMenuFunction_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	/*menu ani back
	*/
	[self pauseSchedulerAndActions];

	/*layer option
	*/
	g_LayerMenuOption = [[layer_menu_option alloc] initLayerMenuOption];
	[g_SceneMenu addChild:g_LayerMenuOption z:1];
	[g_LayerMenuOption release];
	g_LayerMenuOption.positionInPixels = CGPointZero;
}

- (void)CallBackMenuItemGameCenter:(id)Sender
{
	return;

	if (flagLockMenuFunction_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();
}

- (void)CallBackMenuItemMoreGames:(id)Sender
{
	if (flagLockMenuFunction_)
	{
		return;
	}

	/*effect
	*/
	EffectButton();

	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.com/apps/lonicera/id490396087"]];
}

- (void)CallBackItemFunctionAniOver:(id)Sender
{
	flagLockItemFunction_ = false;

	[itemFunction_ runAction:[CCHide action]];
	[itemOption_ runAction:[CCShow action]];
	[itemGameCenter_ runAction:[CCShow action]];
	[itemMoreGames_ runAction:[CCShow action]];

	[itemFunctionSprite_ setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithUTF8String:g_CfgMenuMainAniFunction.path_table.back().c_str()]]];

	[self schedule:@selector(tickFunctionBack:) interval:10.0f];
}

- (void)CallBackItemFunctionBackAniOver:(id)Sender
{
	flagLockItemFunction_ = false;

	[itemFunctionSprite_ setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithUTF8String:g_CfgMenuMainItemFunction.path_normal]]];
}

@end